const xlsx = require('node-xlsx');
const fs = require("fs");
const encryption = require("./encryption");

exports.user = function (path) {
    const sheets = xlsx.parse(fs.readFileSync(path));
    const test = [];
    // console.log(JSON.stringify(sheets));
    // 从 sheets 对象中取出 sheet 数组
    sheets.forEach(function (sheet) {
        // 判断 sheet 是否为空
        if (sheet['data'].length !== 0) {
            for (let rowId in sheet['data']) {
                //console.log(rowId);
                let row = sheet['data'][rowId];
                // 调整数据格式
                test.push({
                    username: row[2],
                    password: encryption(row[3].slice(-6)),
                    name: row[0],
                    sex: row[1],
                    test: []
                })
            }
        }
    });
    return test;
};

exports.question = function (path) {
    const sheets = xlsx.parse(fs.readFileSync(path));
    const test = [];
    // console.log(JSON.stringify(sheets));
    // 从 sheets 对象中取出 sheet 数组
    sheets.forEach(function (sheet) {
        // 判断 sheet 是否为空
        if (sheet['data'].length !== 0) {
            for (let rowId in sheet['data']) {
                let row = sheet['data'][rowId];
                //console.log(rowId)
                //if (rowId === 1) console.log(row)
                if (rowId !== "1") {
                    test.push({ques: row[0]})
                }else {
                    test.push({ques: row});
                }
                // 调整数据格式
            }
        }
    });
    console.log(test)
    return test;
};